<template>
  <div>
    <!--操作 -->
    <button-area :style="{ paddingLeft: '80px' }">
      <!-- <AjaxButton size="small" type="danger" :click="removeHandler">提报</AjaxButton>
      <AjaxButton size="small" type="danger" :click="removeHandler">VIN检验</AjaxButton>
      <AjaxButton size="small" type="danger" :click="removeHandler">获取置换政策</AjaxButton>
      <AjaxButton size="small" type="danger" :click="removeHandler">处理重审单</AjaxButton>
      <AjaxButton size="small" type="danger" :click="removeHandler">附件信息</AjaxButton>
      <AjaxButton size="small" type="danger" :click="removeHandler">审核记录</AjaxButton> -->
    </button-area>
    <div>
      <FormPanel ref="formPanel" :list="formList" :cols="3" :labelWidth="110" :formType="type" :isSubmitBtn="false" @formChange="formChangeHandle" />
    </div>
    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'right'
      }"
    >
      <el-button @click="closeDialog">关 闭</el-button>
      <AjaxButton v-if="type !== 'show'" :click="saveDialog" type="primary">保 存</AjaxButton>
      <BaseDialog :visible.sync="visible1" title="选择车辆牌照号" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
        <selectLicense @closeLicense="cancelDialogLicense" @close="closeHandlerGZDM" @setCustomer1="setCustomer1" />
      </BaseDialog>
      <BaseDialog :visible.sync="visible2" title="选择产品代码" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
        <selectCPDM @closeCPDM="cancelDialogCPDH" @close="closeHandlerGZDM" @setCustomer2="setCustomer2" />
      </BaseDialog>
    </div>
  </div>
</template>

<script>
import { dictionary } from '@/utils/dictMixin';
import { authority } from '@/utils/authMixin';
// import { addMaintain } from '@/api/claimManagement/business/repair';
import moment from 'moment';

import selectLicense from './selectLicense'; /* 表选牌照号 */
import selectCPDM from './selectCPDM'; /* 表选产品代码 */
export default {
  name: 'lbMaintainModal',
  components: {
    selectLicense,
    selectCPDM
  },
  mixins: [dictionary, authority],
  props: {
    type: {
      type: String,
      default: 'add'
    },
    rowData: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    this.conf = { add: '保 存', edit: '修 改' };
    this.saveData = null;
    return {
      formList: this.createFormList(),
      visible1: false,
      visible2: false
    };
  },
  computed: {
    btnText() {
      return this.conf[this.type];
    }
  },
  created() {
    alert(this.type);
    this.init();
  },
  methods: {
    init() {
      if (this.type === 'edit') {
        // 更新接口需要的记录 ID
        this.applyId = this.rowData.applyId;
        this.formList.forEach(x => {
          x.initialValue = this.rowData[x.fieldName];
        });
      }
    },
    createFormList() {
      return [
        {
          type: 'BREAK_SPACE',
          label: '客户&车辆信息',
          style: {}
        },
        {
          type: 'INPUT',
          label: '订单编号',
          fieldName: 'ddbh',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '客户姓名',
          disabled: true,
          fieldName: 'khxm'
        },
        {
          type: 'INPUT',
          label: '手机号',
          disabled: false,
          fieldName: 'iphone'
        },
        {
          type: 'INPUT',
          label: '行驶证',
          disabled: false,
          fieldName: 'xsz'
        },
        {
          type: 'INPUT',
          label: '身份证',
          disabled: true,
          fieldName: 'sfz'
        },
        {
          type: 'INPUT',
          label: '车牌号',
          fieldName: 'carNumber',
          disabled: false,
          unitRender: () => {
            return <el-button slot="append" icon="el-icon-search" onClick={this.openLicense}></el-button>;
          }
        },
        {
          type: 'INPUT',
          label: 'VIN',
          disabled: true,
          fieldName: 'vin'
        },
        {
          type: 'INPUT',
          label: '车型',
          disabled: true,
          fieldName: 'carModer'
        },
        {
          type: 'INPUT',
          label: '发动机号',
          disabled: true,
          fieldName: 'fdjh'
        },
        // {
        //   type: 'INPUT',
        //   label: '电机号',
        //   disabled: true,
        //   fieldName: 'vQ666666ualTypeName'
        // },
        {
          type: 'DATE',
          label: '销售日期',
          disabled: true,
          dateFormat: 'yyyy-MM-dd',
          fieldName: 'xsDate'
        },
        {
          type: 'INPUT',
          label: '行驶里程',
          disabled: false,
          fieldName: 'xslc'
        },
        // 延保信息
        {
          type: 'BREAK_SPACE',
          label: '延保产品信息',
          style: {}
        },
        {
          type: 'INPUT',
          label: '产品代码',
          fieldName: 'cpdmCode',
          placeholder: '请输入...',
          disabled: false,
          unitRender: () => {
            return <el-button slot="append" icon="el-icon-search" onClick={this.openCPDM}></el-button>;
          }
        },
        {
          type: 'INPUT',
          label: '产品名称',
          disabled: true,
          fieldName: 'cpdmName'
        },
        {
          type: 'SELECT',
          label: '本品周期',
          fieldName: 'bpzq',
          disabled: true,
          itemList: [
            { text: '上传状态1', value: 1001 },
            { text: '上传状态2', value: 1002 }
          ]
        },
        {
          type: 'SELECT',
          label: '产品类型',
          fieldName: 'cpdmType',
          disabled: true,
          itemList: [
            { text: '上传状态1', value: 1001 },
            { text: '上传状态2', value: 1002 }
          ]
        },
        {
          type: 'SELECT',
          label: '核销方式',
          fieldName: 'hxfs',
          disabled: true,
          itemList: [
            { text: '上传状态1', value: 1001 },
            { text: '上传状态2', value: 1002 }
          ]
        },
        {
          type: 'SELECT',
          label: '销售期限',
          fieldName: 'xsqx',
          disabled: true,
          itemList: [
            { text: '上传状态1', value: 1001 },
            { text: '上传状态2', value: 1002 }
          ]
        },
        {
          type: 'INPUT',
          label: '车型',
          disabled: true,
          fieldName: 'carModer'
        },
        {
          type: 'INPUT',
          label: '配置',
          disabled: true,
          fieldName: 'pz'
        },
        {
          type: 'INPUT_NUMBER',
          label: '里程上线(公里)',
          disabled: true,
          fieldName: 'lcsx'
        },
        {
          type: 'INPUT_NUMBER',
          label: '价格',
          disabled: true,
          fieldName: 'jg'
        },
        {
          type: 'SELECT',
          label: '车辆用途',
          disabled: true,
          fieldName: 'clyt',
          itemList: this.createDictList('1511')
          // itemList: [
          //   { text: '上传状态1', value: 1001 },
          //   { text: '上传状态2', value: 1002 }
          // ],
          // rules: [{ required: true, message: '请输入...', trigger: 'change' }]
        }
      ];
    },
    formChangeHandle(val) {
      this.saveData = val;
    },
    async saveDialog() {
      const [err, data] = await this.$refs.formPanel.GET_FORM_DATA();
      alert(JSON.stringify(data));
      if (err) return;
      // this.$refs.formPanel.SUBMIT_FORM();
      // alert(JSON.stringify(this.saveData))
      let res;
      // if ('add' === this.type) {
      //   res = await addMaintain(data);
      // }
      if ('edit' === this.type) {
        res = await updateMaintain({ applyId: this.applyId, ...data });
      }
      if (res.resultCode === 200) {
        this.$notify({
          title: '提示信息',
          message: '保存成功！',
          type: 'success'
        });
        this.closeDialog('refresh');
      }
    },
    // 选择 车牌号 带数据
    setCustomer1(val) {
      console.log('val===' + JSON.stringify(val));
      this.formList.forEach(x => {
        if (x.fieldName === 'khxm') {
          // 客户姓名
          x.initialValue = val[0].khxm;
        }
        if (x.fieldName === 'iphone') {
          // 手机号
          x.initialValue = val[0].iphone;
        }
        if (x.fieldName === 'sfz') {
          // 身份证
          x.initialValue = val[0].sfz;
        }
        if (x.fieldName === 'carNumber') {
          // 牌照号
          x.initialValue = val[0].carNumber;
        }
        if (x.fieldName === 'vin') {
          // VIN
          x.initialValue = val[0].vin;
        }
        if (x.fieldName === 'carModer') {
          // 车型
          x.initialValue = val[0].carModer;
        }
        if (x.fieldName === 'fdjh') {
          // 发动机号
          x.initialValue = val[0].fdjh;
        }
        // if (x.fieldName === 'vin28') {//电机号
        //   x.initialValue = val[0].vin;
        // }
        if (x.fieldName === 'xsDate') {
          // 销售日期
          x.initialValue = val[0].xsDate;
        }
      });
      this.cancelDialog('closeLicense'); // 关闭  新车底盘号  弹出框
      this.formList = [...this.formList];
    },

    // 选择产品代码带数据
    setCustomer2(val) {
      console.log('val===' + JSON.stringify(val));
      this.formList.forEach(x => {
        if (x.fieldName === 'cpdmCode') {
          // 产品代码
          x.initialValue = val[0].cpdmCode;
        }
        if (x.fieldName === 'cpdmName') {
          // 产品名称
          x.initialValue = val[0].cpdmName;
        }
        if (x.fieldName === 'bpzq') {
          // 产品周期
          x.initialValue = val[0].bpzq;
        }
        if (x.fieldName === 'cpdmType') {
          // 产品类型
          x.initialValue = val[0].cpdmType;
        }
        if (x.fieldName === 'hxfs') {
          // 核销方式
          x.initialValue = val[0].hxfs;
        }
        if (x.fieldName === 'carModer') {
          // 车型
          x.initialValue = val[0].carModer;
        }
        if (x.fieldName === 'pz') {
          // 配置
          x.initialValue = val[0].pz;
        }
        if (x.fieldName === 'xsqx') {
          // 销售期限
          x.initialValue = val[0].xsqx;
        }
        if (x.fieldName === 'lcsx') {
          // 里程上限(公里)
          x.initialValue = val[0].lcsx;
        }
        if (x.fieldName === 'jg') {
          // 价格
          x.initialValue = val[0].jg;
        }
        if (x.fieldName === 'clyt') {
          // 车辆用途
          x.initialValue = val[0].clyt;
        }
      });
      this.cancelDialog('closeCPDM'); // 关闭  产品代码  弹出框
      this.formList = [...this.formList];
    },
    // 车牌号  弹出框
    async openLicense() {
      this.visible1 = true;
    },
    // 产品代码  弹出框
    async openCPDM() {
      this.visible2 = true;
    },

    cancelDialogLicense() {
      this.visible1 = false;
    },
    cancelDialogCPDH() {
      this.visible2 = false;
    },

    cancelDialog(val) {
      if (val == 'closeLicense') {
        this.visible1 = false;
      }
      if (val == 'closeCPDM') {
        this.visible2 = false;
      }
    },
    closeDialog(param) {
      this.$emit('close', false, param);
    }
  }
};
</script>

<style lang="less" scoped></style>
